package framework.selfdemo.com.draw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.OvershootInterpolator;
import android.widget.OverScroller;

/**
 * Created by zhangruntao on 17/1/3.
 */

public class MyView extends View{

    //初始画笔
    private Paint mPaint;

    //默认字体大小
    private float mDefaultTextSize;

    private int mLastY;
    private int mLastX;

    private OverScroller mScroller;

    public MyView(Context context) {
        this(context,null);
    }

    public MyView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint();
        //抗锯齿
        mPaint.setAntiAlias(true);

        mDefaultTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,16,context.getResources().getDisplayMetrics());

        mScroller = new OverScroller(context,new OvershootInterpolator());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //绘制画布颜色
        canvas.drawColor(Color.parseColor("#bdbdbd"));

        //大小
        mPaint.setTextSize(mDefaultTextSize);
        //颜色
        mPaint.setColor(Color.RED);
        //下划线
        mPaint.setUnderlineText(true);
        //删除线
        mPaint.setStrikeThruText(true);
        //缩放
        mPaint.setTextScaleX(2);
        //倾斜度
        mPaint.setTextSkewX(0.3f);
        //透明度
        mPaint.setAlpha(127);
        //画笔样式
        mPaint.setStyle(Paint.Style.FILL);
        //绘制文字
        canvas.drawText("zhangruntao",0,100,mPaint);

        //重置画笔
        mPaint.reset();
        mPaint.setColor(Color.RED);
        //绘制矩形
        canvas.drawRect(50,150,300,300,mPaint);

        mPaint.setColor(Color.BLUE);
        //设置画笔宽度，绘制点的时候必须指定画笔宽度
        mPaint.setStrokeWidth(50);
        //设置画笔为圆形
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        //绘制一个点
        canvas.drawPoint(100,400,mPaint);
        //绘制多个点
        canvas.drawPoints(new float[]{100,500,200,500,300,500},mPaint);

        mPaint.setStrokeWidth(10);
        //绘制单条线
        canvas.drawLine(50,600,1000,600,mPaint);
        //绘制多条线
        canvas.drawLines(new float[]{50,700,1000,700,50,750,1000,750},mPaint);

        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        //绘制圆
        canvas.drawCircle(100,850,50f,mPaint);

        RectF rectF = new RectF(100,1000,200,1200);
        //根据RectF绘制矩形
        canvas.drawRect(rectF,mPaint);

        //绘制圆角矩形
        canvas.drawRoundRect(100,1300,1000,1500,50,150,mPaint);

        //绘制椭圆
        canvas.drawOval(100,1550,500,1700,mPaint);

        //绘制弧形
        canvas.drawArc(100,1700,500,1900,0,90,true,mPaint);
        canvas.drawArc(100,2000,500,2100,-90,180,false,mPaint);

        //绘制路径
        Path path = new Path();
        path.moveTo(200,2200);
        path.lineTo(100,2400);
        path.lineTo(300,2400);
        path.lineTo(150,2200);
        path.lineTo(300,2400);
        //path.close();
        canvas.drawPath(path,mPaint);

        //根据路径绘制文字
        mPaint.reset();
        mPaint.setTextSize(mDefaultTextSize);
        canvas.drawPosText("zrt",new float[]{0,2500,0,2550,0,2600},mPaint);
        Path textOnPath = new Path();
        textOnPath.moveTo(300,2500);
        textOnPath.arcTo(300,2500,500,2750,180,180,true);
        canvas.drawTextOnPath("于佳琪是大傻逼",textOnPath,0,0,mPaint);

        //save和restore
        mPaint.setColor(Color.argb(55,33,33,33));
        canvas.drawRect(0,0,100,100,mPaint);
        canvas.save();
        canvas.rotate(30);
        canvas.drawRect(0,100,100,200,mPaint);
        canvas.restore();
        mPaint.setColor(Color.argb(55,166,66,66));
        canvas.drawRect(150,0,250,100,mPaint);

        //贝塞尔曲线
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(10);
        Path bezierPath = new Path();
        bezierPath.moveTo(0,3000);
        //二次贝塞尔
        bezierPath.rQuadTo(400,100,500,300);
        bezierPath.moveTo(700,3000);
        //三次贝塞尔
        bezierPath.rCubicTo(400,50,200,300,1000,300);
        canvas.drawPath(bezierPath,mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()){
            case MotionEvent.ACTION_DOWN:
                mLastY = (int) event.getY();
                mLastX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                int dy = (int)event.getY() - mLastY;
                int dx = (int)event.getX() - mLastX;
                scrollBy(-dx,-dy);
                mLastY = (int) event.getY();
                mLastX = (int) event.getX();
                break;
        }
        return true;
    }

    public void resetScroll(){
        int dx = getScrollX() < 0 ? Math.abs(getScrollX()) : -getScrollX();
        int dy = getScrollY() < 0 ? Math.abs(getScrollY()) : -getScrollY();
        mScroller.startScroll(getScrollX(),getScrollY(),dx,dy,500);
        postInvalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();

        if(mScroller.computeScrollOffset()){
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }
}
